<template>
    <!-- 绑定支付宝页 -->
    <div class="bind">
        <div class="form">
            <div>
                <input type="text" v-model="account" placeholder="请输入支付宝账号（手机/邮箱）">
            </div>
            <div class="relative">
                <input type="text" v-model="code" placeholder="请输入验证码">
                <span class="sendcode-btn absolute" :class="{'btn_code_grey':sendtext != '发送验证码'}" style="right: .3rem; top: .12rem;" v-text="sendtext" @click="sendcode(type==0?4:5)"></span>
            </div>
        </div>
        <div class="btn_account" @click="bind">{{type==0?'绑定':'修改'}}</div>
    </div>
</template>
<script>
    export default{
        data:function(){
            return{
                account:'',
                type:0,
                code:'',
                mobile:localStorage.mobile,
                sendtext:'发送验证码',
                timenum:60,
                timefun:''
            }
        },
        mounted(){
            this.type = this.getQueryString("type");
            if(this.type){
                this.getAccount();
            }
        },
        methods:{
            getmobile:function(num){
                if(num){
                    var str = num.toString();
                    var str1 = str.substring(0,3);
                    var str2 = str.substring(7);
                    var str3 = str1+'****'+str2;
                    return str3
                }
            },
            trim:function(str,is_global){
                var result;
                result = str.replace(/(^\s+)|(\s+$)/g,"");
                if(is_global.toLowerCase()=="g"){
                    result = result.replace(/\s/g,"");
                 }
                return result;
            },
            bind(){
                let url = 'api/adder/bindaccount';
                let message = "绑定成功！";
                let enterUrl = "/checkidentity";
                if(this.type == 1){
                   url = 'api/adder/updateaccount';
                   message = "修改成功！";
                   enterUrl = "/account";
                }
                if(!this.trim(this.account,'g')){
                    this.confirm(`请输入支付宝账号（手机/邮箱）`);
                }else if(!this.trim(this.code,'g')){
                    this.confirm(`请输入验证码`);
                }else{
                    this.post({
                        url:url,
                        data:{
                            account:this.trim(this.account,'g'),
                            code:this.trim(this.code,'g')
                        }
                    }).then(res=>{
                        this.confirm(message);
                        setTimeout(()=>{
                           this.$router.push(enterUrl);
                        },2000);
                    })
                }
            },
            getAccount(){
                this.get({
                    url:"api/user/withdrawaldata?currency=charm"
                }).then(res=>{
                    this.account = res.account;
                })
            },
            //倒计时
            timeOut:function(){
                if (this.timenum <= 0) {
                    clearInterval(this.timefun);
                    this.timefun = '';
                    this.timenum = 60;
                    this.sendtext = "发送验证码";
                    return;                    
                }else{
                    this.timenum -= 1;
                    this.sendtext = this.timenum+"s后重发";                    
                }
            },
            //发送验证码接口
            sendcode(type){
                if(this.sendtext == "发送验证码"){
                    this.sendtext = "发送中...";
                    this.post({
                        url:"/api/user/sendcode",
                        data:{
                            mobile:this.mobile,
                            type:type
                        }
                    }).then(response=>{
                        var formatMobile = this.getmobile(this.mobile);
                        this.confirm(`验证码已发送您的手机${formatMobile},请注意查收！`);
                        this.timefun = setInterval(()=>{
                            this.timeOut();
                        }, 1000); 
                    }).catch(response=>{
                        this.sendtext = "发送验证码"
                    })
                }
            }
        }
    }
</script>
<style type="text/css">
    @import url('./account.css');
</style>
